package Model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

/**
 * Game Class
 * 
 * @author Jose Filipe M Santos - 7277502
 * @author Matheus Martins Teixeira - 7277482
 */
public class Game implements Serializable {

    private Date date;
    private Team teamHome;
    private Team teamVisitor;
    private int scoreHome;
    private int scoreVisitor;

    /**
     * Game class: manage the game data and simulate game
     * 
     * @param pDat  The day of the match
     * @param pHome  Home team
     * @param pVisi  Visitor team
     */
    public Game(Date pDat, Team pHome, Team pVisi) {
	date = pDat;
	teamHome = pHome;
	teamVisitor = pVisi;
	scoreHome = 0;
	scoreVisitor = 0;
    }

    /**
     * Increases the goal number of home team
     */
    public void goalHome() {
	scoreHome++;
    }

    /**
     * Increases the goal number of visitors team
     */
    public void goalVisitor() {
	scoreVisitor++;
    }

    /**
     * Return score of the match
     * @return Home 0 x 0 Visitors
     */
    public String getscore() {
	return (teamHome.getName() + " " + Integer.toString(scoreHome) + " x " + Integer.toString(scoreVisitor) + " " + teamVisitor.getName());
    }

    /**
     * Starts the simulation
     */
    public void simulateGame() {

	int A, B, chances;
	/* tries of teamHome */
	for (chances = 10; chances > 0; chances--) {
	    A = (int) (Math.random() * average(teamHome.getStartingAttackPlayers()) * teamHome.getStartingAttackPlayers().size()) % 100;
	    B = (int) (Math.random() * ((average(teamVisitor.getStartingGkPlayers()) + average(teamVisitor.getStartingGkPlayers())) / 2) * (teamVisitor.getStartingDefensePlayers().size() + 1)) % 150;

	    if (A > B) {
		goalHome();
		if ((int) (Math.random() * 100) % 3 == 1) {
		    ((PlayerDef) teamHome.getStartingDefensePlayers().get((int) (Math.random() * 100 % teamHome.getStartingDefensePlayers().size()))).sumGoal();
		} else {
		    ((PlayerAt) teamHome.getStartingAttackPlayers().get((int) (Math.random() * 100 % teamHome.getStartingAttackPlayers().size()))).sumGoal();
		}
	    }
	}
	/* tries of teamVisitors */
	for (chances = 10; chances > 0; chances--) {
	    A = (int) (Math.random() * average(teamVisitor.getStartingAttackPlayers()) * teamVisitor.getStartingAttackPlayers().size()) % 100;
	    B = (int) (Math.random() * ((average(teamHome.getStartingGkPlayers()) + average(teamHome.getStartingGkPlayers())) / 2) * (teamHome.getStartingDefensePlayers().size() + 1)) % 150;
	    if (A > B) {
		goalVisitor();
		if ((int) (Math.random() * 100) % 3 == 1) {
		    ((PlayerDef) teamVisitor.getStartingDefensePlayers().get((int) (Math.random() * 100 % teamVisitor.getStartingDefensePlayers().size()))).sumGoal();
		} else {
		    ((PlayerAt) teamVisitor.getStartingAttackPlayers().get((int) (Math.random() * 100 % teamVisitor.getStartingAttackPlayers().size()))).sumGoal();
		}
	    }
	}
	if (this.scoreHome > this.scoreVisitor) {
	    this.teamHome.sumWin();
	    this.teamVisitor.sumLost();
	} else if (this.scoreHome == this.scoreVisitor) {
	    this.teamHome.sumDraw();
	    this.teamVisitor.sumDraw();
	} else if (this.scoreHome < this.scoreVisitor) {

	    this.teamHome.sumLost();
	    this.teamVisitor.sumWin();
	}

    }

    /**
     * Return home team
     * @return home Team object
     */
    public Team getHomeTeam() {
	return teamHome;
    }

    /**
     * Return visitor team
     * @return Visitor Team object
     */
    public Team getVisitorTeam() {
	return teamVisitor;
    }

    /**
     * Returns average hability of the players
     * @return The average of the players
     */
    private int average(ArrayList<Player> a) {

	Iterator<Player> it = a.iterator();
	int num = 0;
	int sum = 0;
	Player p;
	while (it.hasNext()) {
	    p = it.next();
	    sum += p.getHability();
	    num++;
	}

	if (num > 0) {
	    return (sum / num);
	} else {
	    return 0;
	}
    }

    public int getScoreHome() {
	return scoreHome;
    }

    public int getScoreVisitor() {
	return scoreVisitor;
    }
    
    public void setHomeTeam(Team home){
	teamHome = home;
    }
    
    public void setVisitorsTeam(Team vis){
	teamVisitor = vis;
    }
}
